package com.tongzhan.controller;

import com.tongzhan.base.BaseController;
import com.tongzhan.base.Return;
import com.tongzhan.entity.xiangxian.ExamplePoint;
import com.tongzhan.entity.xiangxian.UserInfo;
import com.tongzhan.utils.DateSwitcher;
import com.tongzhan.utils.SnowFlakeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author hongjianbin
 * @date 2020/7/20
 */
@RequestMapping("/baseInfo")
@RestController
public class BaseInfoController extends BaseController {
    /**
     * 添加示范点
     *
     * @param name    姓名
     * @param address 地址
     * @param url     图片
     * @param info    信息
     * @return
     */
    @PostMapping("/addExamplepoint")
    public Return addExamplepoint(String name, String address, String url, String info, String time, String userId, String addUserId, String addUserName) throws ParseException {
        if (StringUtils.isEmpty(name) || "".equals(name.trim())
                || StringUtils.isEmpty(address) || "".equals(address.trim())
                || StringUtils.isEmpty(info) || "".equals(info.trim())
                || StringUtils.isEmpty(addUserId) || "".equals(addUserId.trim())
                || StringUtils.isEmpty(addUserName) || "".equals(addUserName.trim())) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (name.trim().length() > 10) {
            return Return.getErrorResult("名称过长");
        }
        if (info.trim().length() > 500) {
            return Return.getErrorResult("简介过长");
        }
        if (address.trim().length() > 50) {
            return Return.getErrorResult("地点输入过长");
        }
        ExamplePoint examplePoint = new ExamplePoint();
        SnowFlakeUtils snowFlake = new SnowFlakeUtils(3, 3);
        examplePoint.setType(3);
        examplePoint.setEpid(String.valueOf(snowFlake.nextId()));
        examplePoint.setName(name.trim());
        examplePoint.setAddress(address.trim());
        examplePoint.setInfo(info.trim());
        examplePoint.setTime(DateSwitcher.string2Date(time));
        examplePoint.setUrl(url);
        examplePoint.setIs_del(0);
        examplePoint.setAddUserId(userId);
        examplePoint.setAddUserId(addUserId);
        examplePoint.setAddUserName(addUserName);

        if (name != null) {
            long L = baseInfoService.findExamplepointByName(name, 3);
            if (L > 0) {
                return Return.getErrorResult("示范点已存在!");
            }
        }

        long l = baseInfoService.addExamplepoint(examplePoint);
        if (l > 0) {
            return Return.getSuccessResult("新增成功!");
        }
        return Return.getErrorResult("新增失败!");
    }

    /**
     * 示范点详情
     *
     * @param epid
     * @return
     */
    @PostMapping("/examplePointDetail")
    public Return getdetail(String epid) {
        if (epid == null) {
            return Return.getErrorResult("缺少id参数!");
        }
        ExamplePoint point = baseInfoService.getdetail(epid);
        return Return.getSuccessResult(point);
    }

    /**
     * 示范点列表
     *
     * @param name 姓名
     * @param page
     * @param size
     * @return
     */
    @PostMapping("/epList")
    public Return epList(String name, Integer page, Integer size) {
        page = page == null ? 0 : page - 1;
        Page<ExamplePoint> list = baseInfoService.epList(name, page, size);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list.getContent());
        map.put("total", list.getTotalElements());
        map.put("totalpage", list.getTotalPages());
        return Return.getSuccessResult(map);
    }

    /**
     * 乡贤的用户新增
     *
     * @param name
     * @param type
     * @param sex
     * @param age         出生日期
     * @param phone
     * @param id_card
     * @param address     工作单位
     * @param front_url
     * @param back_url
     * @param streetCode
     * @param villageCode
     * @param typeName    职务
     * @param addUserName 会内任职情况
     * @return
     */
    @PostMapping("/addUserinfo")
    public Return addUserinfo(String name, Integer type, String sex, String age, String phone,
                              String id_card, String address, String front_url, String back_url,
                              String streetCode, String villageCode,
                              String typeName, String addUserName) {

        if (name.trim().length() > 10) {
            return Return.getErrorResult("用户名过长");
        }

        if (address.trim().length() > 50) {
            return Return.getErrorResult("地点输入过长");
        }
        UserInfo userInfo = new UserInfo();
        SnowFlakeUtils snowFlake = new SnowFlakeUtils(4, 4);
        userInfo.setUserId(String.valueOf(snowFlake.nextId()));
        userInfo.setAddress(address);
        userInfo.setAge(age);
        userInfo.setBack_url(back_url);
        userInfo.setFront_url(front_url);
        userInfo.setName(name);
        userInfo.setType(type);
        userInfo.setPhone(phone);
        userInfo.setSex(sex);
        userInfo.setIs_del(0);
        userInfo.setId_card(id_card);
        userInfo.setStreetCode(streetCode);
        userInfo.setVillageCode(villageCode);
        userInfo.setTypeName(typeName);
        userInfo.setAddUserName(addUserName);
        long l = baseInfoService.addUserinfo(userInfo);
        if (l > 0) {
            return Return.getSuccessResult("新增成功!");
        }
        return Return.getErrorResult("新增失败!");
    }

    /**
     * 用户详情
     *
     * @param userId
     * @return
     */
    @PostMapping("/userInfoDetail")
    public Return userInfoDetail(String userId) {
        if (userId == null) {
            return Return.getErrorResult("缺少id参数!");
        }
        UserInfo userInfo = baseInfoService.userInfoDetail(userId);
        return Return.getSuccessResult(userInfo);
    }

    /**
     * @param name 用户姓名
     * @param page
     * @param size
     * @return
     */
    @PostMapping("/userInfoList")
    public Return userInfoList(String name, Integer page, Integer size, String streetCode, String villageCode) {
        page = page == null ? 0 : page - 1;
        Page<UserInfo> list = baseInfoService.userInfoList(name, page, size, streetCode, villageCode);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list.getContent());
        map.put("total", list.getTotalElements());
        map.put("totalpage", list.getTotalPages());
        return Return.getSuccessResult(map);
    }

}
